/**
 * @author xuzj create 2008-06-20
 * @text 菜单权限-菜单(菜单权限菜单关系管理)
 */
var MenuFnuc = {
    init: function(){
        // DWR方法
        var dwr = {
            pageFunction: functionDwr.pageFunction,
            getTismFunction: functionDwr.getTismFunction,
            createFunction: functionDwr.createFunction,
            updateFunction: functionDwr.updateFunction,
            realMenus: functionDwr.updateFunctionAndExt,
            getMainMenusOfWorker: authDwr.getMainMenusOfWorker,
            getRealMenus: functionDwr.getTismFunctionAndExt
        };
        
        // 增加按钮
        var addBtn = new CQCIS.Button({
            text: '菜单权限增加 (<u>A</u>)',
            iconCls: 'btn_insert',
            handler: function(){
                functionWin.load();
            }
        });
        
        // 修改按钮
        var editBtn = new CQCIS.Button({
            text: '菜单权限修改 (<u>E</u>)',
            iconCls: 'btn_modify',
            handler: function(){
                if (functionGrid.getSelectedRow()) {
                    functionWin.load(functionGrid.getSelectedRow().funcId);
                } else {
                    CQCIS.msg("请选择要修改的功能点");
                }
            }
        });
        
        // 关联按钮
        var realBtn = new CQCIS.Button({
            text: '菜单权限关联 (<u>M</u>)',
            iconCls: 'btn_config',
            handler: function(){
                var checkFunction = functionGrid.getSelectedRow();
                if (checkFunction && checkFunction.funcId) {
                    realMenuWin.funcId = checkFunction.funcId;
                    dwr.getRealMenus(checkFunction.funcId, function(lv){
                        menuTree.checkedRows = lv.menus;
                        realMenuWin.show();
                        menuTree.root.reload();
                    });
                } else {
                    CQCIS.msg("请选择要关联的功能点！");
                }
            }
        });
        
        // 菜单权限列表字段对象
        var functionListMeta = [{
            id: 'funcId',
            field: 'funcId',
            desc: '菜单权限ID',
            type: 'int',
            hidden: true
        }, {
            field: 'funcName',
            desc: '菜单权限名称',
            sort: 1,
            width: 140
        }, {
            field: 'state',
            desc: '菜单权限状态',
            width: 60,
            renderer: function translateFun(value){
                var str = "";
                switch (value) {
                    case '0':
                        str = "无效";
                        break;
                    case '1':
                        str = "有效";
                        break;
                }
                return str;
            }
        }, {
            field: 'funcDesc',
            desc: '菜单权限说明',
            sort: 1,
            width: 220
        }];
        
        // 菜单权限字段对象
        var functionMeta = [{
            id: 'funcId',
            field: 'funcId',
            desc: 'ID',
            xtype: 'hidden'
        }, {
            field: 'funcLevel',
            desc: '菜单权限等级',
            xtype: 'hidden',
            obj: {
                value: '0'
            }
        }, {
            field: 'funcName',
            desc: '菜单权限名称',
            vtype: 'valText',
            obj: {
                emptyText: '菜单权限名称只能由中文、英文或数字组成',
                allowBlank: false
            }
        }, {
            field: 'state',
            desc: '菜单权限状态',
            type: 'int',
            xtype: 'acombo',
            obj: {
                ds: {
                    data: [[0, '无效'], [1, '有效']],
                    fields: ['key', 'label']
                },
                value: 1
            }
        }, {
            field: 'funcDesc',
            desc: '菜单权限说明',
            xtype: 'area',
            obj: {
                height: 80
            }
        }];
        
        // 菜单权限信息表单
        var functionForm = new CQCIS.form.DWRForm({
            dwr: {
                load: dwr.getTismFunction,
                save: dwr.createFunction,
                update: dwr.updateFunction
            },
            metas: functionMeta,
            listeners: {
                'saveafter': function(funcId){
                    CQCIS.confirm('你是否需要配置与该功能点相关的菜单？', function(param){
                        if (param == 'yes') {
                            realMenuWin.funcId = functionWin.action == 'edit' ? functionGrid.getSelectedRow().funcId : funcId;
                            dwr.getRealMenus(realMenuWin.funcId, function(lv){
                                menuTree.checkedRows = lv.menus;
                                realMenuWin.show();
                                menuTree.root.reload();
                            });
                        } else {
                            CQCIS.msg("菜单权限保存成功！");
                        }
                        functionGrid.refresh();
                        functionWin.hide();
                    }, this);
                }
            }
        });
        
        // 菜单权限信息窗口
        var functionWin = new CQCIS.form.WinForm({
            title: '菜单权限信息',
            width: 390,
            height: 220,
            items: functionForm
        });
        
        // 增加权限右键菜单项
        var addMenu = new Ext.menu.Item({
            iconCls: 'menu_insert',
            id: 'addMenu',
            text: '菜单权限增加',
            handler: addBtn.handler
        });
        
        // 修改权限右键菜单项
        var editMenu = new Ext.menu.Item({
            iconCls: 'menu_modify',
            id: 'editMenu',
            text: '菜单权限修改',
            handler: editBtn.handler
        });
        
        // 关联权限右键菜单项
        var realMenu = new Ext.menu.Item({
            iconCls: 'menu_config',
            id: 'realMenu',
            text: '菜单权限关联',
            handler: realBtn.handler
        });
        
        // 权限列表右键菜单
        var gridMenu = new Ext.menu.Menu({
            id: 'gridMenu',
            items: [addMenu, editMenu, realMenu]
        });
        
        
        // 菜单权限列表
        var functionGrid = new CQCIS.grid.PageGrid({
            firstColumn: 'radio',
            dwr: {
                call: dwr.pageFunction
            },
            metas: functionListMeta,
            initload: true,
            tbar: ['-', addBtn, editBtn, realBtn],
            bbar: true,
            listeners: {
                'rowdblclick': editBtn.handler,
                'rowcontextmenu': function(grid, rowIndex, e){
                    e.stopEvent();
                    gridMenu.showAt(e.getXY());
                }
            }
        });
        functionGrid.render();
        
        // 菜单树
        var menuTree = new CQCIS.tree.ColumnTree({
            dwrCall: dwr.getMainMenusOfWorker,
            rootId: CQCIS.SM.MENU_ROOT_CODE,
            nodeMeta: {
                leaf: 'menuIdend'
            },
            rootText: '菜单',
            columns: [{
                id: 'menuCode',
                header: '菜单名称',
                width: 250,
                dataIndex: 'menuName'
            }, {
                header: '菜单说明',
                width: 250,
                dataIndex: 'menuDesc'
            }]
        });
        
        // 关联菜单窗口
        var realMenuWin = new CQCIS.form.WinForm({
            title: '菜单权限关联菜单',
            width: 530,
            height: 400,
            items: menuTree,
            listeners: {
                'submit': function(){
                    var checkMenus = menuTree.getSelectedRows();
                    dwr.realMenus(realMenuWin.funcId, checkMenus, function(){
                        CQCIS.msg("功能点关联菜单成功！");
                        realMenuWin.hide();
                    });
                }
            }
        });
    }
};

Ext.onReady(MenuFnuc.init, MenuFnuc);
